<template>
  <div class="page">
    <el-form
      ref="form"
      class="form"
      :model="model"
      :rules="rules"
      label-width="90"
    >
      <el-form-item label="Username" prop="username">
        <el-input v-model="model.username" placeholder="请输入 Username" />
      </el-form-item>

      <el-form-item label="Password" prop="password">
        <el-input
          v-model="model.password"
          type="password"
          placeholder="请输入 Password"
        />
      </el-form-item>

      <el-form-item label="VerifyCode" prop="verifyCode">
        <div class="code-box">
          <el-input
            v-model="model.verifyCode"
            placeholder="请输入 VerifyCode"
            @keyup.enter="handleLogin"
          />

          <Captcha ref="captchaRef" />
        </div>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :loading="loading" @click="handleLogin">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Captcha from "./components/Captcha.vue";
import { ElMessage } from "element-plus";
import { loginApi } from "@/api/login";

export default {
  components: {
    Captcha,
  },

  data() {
    return {
      // 登录中
      loading: false,
      // 表单数据
      model: {
        username: "admin",
        password: "123456",
        verifyCode: "",
      },
      // 表单校验规则
      rules: {
        username: [
          { required: true, message: "请输入 Username" },
          // { min: 5, message: "最小长度为5位" },
          { max: 10, message: "最大长度为10位" },
          // { type: "email", message: "请输入邮箱格式" },
        ],
        password: [
          { required: true, message: "请输入 Password" },
          // { pattern: /^1\d{10}$/, message: "请满足正则" },
        ],
        verifyCode: [{ required: true, message: "请输入 VerifyCode" }],
      },
    };
  },

  methods: {
    /**
     * 处理登录
     */
    handleLogin() {
      // 触发表单的校验
      this.$refs.form.validate(async (flag) => {
        // 表单校验失败
        if (!flag) return;

        try {
          // 调用登录接口
          this.loading = true;

          // 派发登录动作
          await this.$store.dispatch("user/login", {
            ...this.model,
            captchaId: this.$refs.captchaRef.captchaId,
          });

          // 获取用户信息 1107 在全局前置守卫中派发了。
          // await this.$store.dispatch("user/getInfo");

          // await loginApi({
          //   ...this.model,
          //   captchaId: this.$refs.captchaRef.captchaId,
          // });

          console.log("登录成功");

          ElMessage({
            type: "success",
            message: "登录成功",
          });

          // 取出url地址中的 redirect 参数的值
          const redirect = this.$route.query.redirect || "/";

          this.$router.replace(redirect);
        } catch (error) {
          console.log("校验失败or登录失败");

          // 让 Captcha 组件重新获取数据
          this.$refs.captchaRef.refresh();
          this.$refs.form.resetFields();
          // this.model.verifyCode = ''
        } finally {
          this.loading = false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.page {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  background-color: #ccc;
}

.form {
  padding: 16px;
  width: 360px;
  border-radius: 15px;
  background-color: #fff;
}

.code-box {
  display: flex;
  height: 32px;
}

</style>
